<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="农事类型" prop="farmingType">
        <el-select v-model="queryParams.farmingType" placeholder="请选择农事类型" clearable style="width: 240px">
          <el-option
            v-for="dict in peach_agriculture_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="作业方式" prop="operatingMode">
        <el-select v-model="queryParams.operatingMode" placeholder="请选择作业方式" clearable style="width: 240px">
          <el-option
            v-for="dict in peach_agriculture_manner"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作日期" prop="operatingDate">
        <el-date-picker clearable
          v-model="queryParams.operatingDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择操作日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="农事描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入农事描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="operator">
        <el-select v-model="queryParams.operator" placeholder="请选择操作人员" clearable style="width: 240px">
          <el-option
            v-for="dict in peach_farming_operator"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
       <el-form-item label="添加人员" prop="userId">
          <el-select v-model="queryParams.userId" placeholder="请选择添加人员" clearable style="width: 240px">
            <el-option  v-for="user in userList"
            :key="user.userId"
            :label="'用户id:' + user.userId + ',昵称' + user.nickName"
            :value="user.userId"></el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="地块" prop="plot">
        <el-select v-model="queryParams.plot" placeholder="请选择地块" clearable style="width: 240px">
          <el-option
            v-for="p in plotList"
            :key="p.id"
            :label="p.name"
            :value="p.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['peach:farming:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['peach:farming:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['peach:farming:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['peach:farming:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="farmingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="农事类型" align="center" prop="farmingType">
        <template #default="scope">
          <dict-tag :options="peach_agriculture_type" :value="scope.row.farmingType"/>
        </template>
      </el-table-column>
      <el-table-column label="作业方式" align="center" prop="operatingMode">
        <template #default="scope">
          <dict-tag :options="peach_agriculture_manner" :value="scope.row.operatingMode"/>
        </template>
      </el-table-column>
      <el-table-column label="操作日期" align="center" prop="operatingDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operatingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="农事描述" align="center" prop="description" />
      <el-table-column label="操作人员id" align="center" prop="operator">
        <template #default="scope">
          <dict-tag :options="peach_farming_operator" :value="scope.row.operator"/>
        </template>
      </el-table-column>
      <el-table-column label="照片地址" align="center" prop="imgUrls" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imgUrls" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="添加人员id" align="center" prop="userId" />
      <el-table-column label="添加人员昵称" align="center" prop="nickName" />
      <el-table-column label="地块序号" align="center" prop="plot" />
      <el-table-column label="地块位置" align="center" prop="location" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['peach:farming:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['peach:farming:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农事对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="farmingRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="农事类型" prop="farmingType">
          <el-select v-model="form.farmingType" placeholder="请选择农事类型">
            <el-option
              v-for="dict in peach_agriculture_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作业方式" prop="operatingMode">
          <el-select v-model="form.operatingMode" placeholder="请选择作业方式">
            <el-option
              v-for="dict in peach_agriculture_manner"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作日期" prop="operatingDate">
          <el-date-picker clearable
            v-model="form.operatingDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择操作日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="农事描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入农事描述" />
        </el-form-item>
        <el-form-item label="操作人员" prop="operator">
          <el-select v-model="form.operator" placeholder="请选择操作人员">
            <el-option
              v-for="dict in peach_farming_operator"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="照片地址" prop="imgUrls">
          <image-upload v-model="form.imgUrls"/>
        </el-form-item>
        <el-form-item label="地块" prop="plot">
          <el-select v-model="form.plot" placeholder="请选择地块" clearable style="width: 240px">
            <el-option
              v-for="p in plotList"
              :key="p.id"
              :label="p.name"
              :value="p.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="添加人员" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择添加人员" clearable :style="{width: '100%'}">
            <el-option  v-for="user in userList"
            :key="user.userId"
            :label="'用户id:' + user.userId + ',昵称' + user.nickName"
            :value="user.userId"></el-option>
          </el-select>
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Farming">
import { listFarming, getFarming, delFarming, addFarming, updateFarming } from "@/api/peach/farming";
import { listPlot } from "@/api/peach/plot";
import { listUser } from "@/api/system/user.js";
import { pl } from "element-plus/es/locales.mjs";
import { ref } from "vue";
const { proxy } = getCurrentInstance();
const { peach_agriculture_type, peach_farming_operator, peach_agriculture_manner } = proxy.useDict('peach_agriculture_type', 'peach_farming_operator', 'peach_agriculture_manner');

const farmingList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const plotList = ref([]);
const userList = ref([])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    farmingType: null,
    operatingMode: null,
    operatingDate: null,
    description: null,
    operator: null,
    imgUrls: null,
    userId: null,
    plot:null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);
listPlot({
  pageSize: 9999,
  pageNum: 1
}).then(response => {
  plotList.value = response.rows;
});
listUser({
  pageSize: 9999,
  pageNum: 1
}).then(res => {
  userList.value = res.rows
})
/** 查询农事列表 */
function getList() {
  loading.value = true;
  listFarming(queryParams.value).then(response => {
    farmingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    farmingType: null,
    operatingMode: null,
    operatingDate: null,
    description: null,
    operator: null,
    imgUrls: null,
    userId: null
  };
  proxy.resetForm("farmingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加农事";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFarming(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改农事";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["farmingRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFarming(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFarming(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除农事编号为"' + _ids + '"的数据项？').then(function() {
    return delFarming(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('peach/farming/export', {
    ...queryParams.value
  }, `farming_${new Date().getTime()}.xlsx`)
}

getList();
</script>
